package com.example.demo.arithmetic;

/**
 * Description: 求幂运算<br/>
 *
 * @author liulonglong
 * @taskId <br/>
 * @Date 2020/3/13 15:46
 * @Version 1.0
 */
public class PowTest {

    public static long pow(long x, int n) {
        if (n == 0) {
            return 1;
        }
        if (n == 1) {
            return x;
        }
        // even
        if (n % 2 == 0) {
            return pow(x, n >>> 1) * pow(x, n >>> 1);
        } else { // odd
            return pow(x, (n - 1) >>> 1) * pow(x, (n - 1) >>> 1) * x;
        }
    }

    public static void main(String[] args) {
        System.out.println(pow(10, 20));
    }
}
